package InetProgram;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpSend {
    public static void main(String[] args) throws IOException, InterruptedException {
        sendData();
    }

    /**
     * 发送
     *
     * @throws IOException
     */
    public static void sendData() throws IOException {
        Scanner sc = new Scanner(System.in);
        /**
         * 发送方对象绑定端口
         * 空参随机
         */
        DatagramSocket ds = new DatagramSocket();
        while (true) {
            System.out.println("writing your message:");
            //准备发送数据
            String s = sc.nextLine();
            if ("886".equals(s)) break;

            byte[] bytes = s.getBytes();
            InetAddress address = InetAddress.getByName("127.0.0.1");
            int port = 10086;
            //打包数据 字节数据，字节长度，发送目的主机，发送目的主机端口
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
            ds.send(dp);
        }
        ds.close();
    }

    public static void sendMultiData() {

    }
}
